package com.lzp.controller.test;

import com.lzp.utils.RedisOperator;
import com.lzp.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 测试Redis操作
 */
@ApiIgnore
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private RedisOperator redis;
    // private RedisTemplate<String, String> redisTemplate;

    @GetMapping("/set")
    public Object set(String key, String value) {
        redis.set(key, value);
        return ServerResponse.createBySuccess();
    }

    @GetMapping("/get")
    public Object get(String key) {
        return redis.get(key);
    }

    @GetMapping("/delete")
    public Object delete(String key) {
        redis.del(key);
        return ServerResponse.createBySuccess();
    }

    /**
     * 批量查询，减少网络连接次数
     *
     * @param keys
     * @return
     */
    @GetMapping("/mget")
    public Object mget(String... keys) {
        List<String> keyList = Arrays.asList(keys);
        return redis.mget(keyList);
    }

    /**
     * 批量查询，管道功能 -- 保持长连接
     */
    @GetMapping("/batchGet")
    public Object batchGet(String... keys) {
        List<String> keyList = Arrays.asList(keys);
        return redis.batchGet(keyList);
    }
}
